<%@ page pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + request.getContextPath();
    request.setAttribute("basePath", basePath); // 将BasePath的内容保存在application属性之中
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>DEMO</title>
    <link rel="stylesheet" href="../../css/bootstrap.min.css"/>
    <link rel="stylesheet" href="../../css/common.css"/>
    <link rel="stylesheet" href="../../css/welcome.css"/>
    <link rel="stylesheet" href="../../css/validationEngine.jquery.css"/><!--表单验证css-->
    <style type="text/css">
        .zhanghao_js_add label.control-label .label_check {
            font-weight: bold;
        }

        .zhanghao_js_add .form-group label.control-label + div {
            display: block;
            border: none;
            border: solid 1px #cdffff;
            background: #f8fffe;
        }

        .zhanghao_js_add .form-group label.control-label + div > label > label {
            color: #333;
        }

        .block {
            display: block;
            padding: 5px 0px 5px 0px;
        }

        .zhanghao_js_add .form-group label.control-label + div > div.switch_box {
            border: solid 1px #bee6e1;
            background: #f0fafa;
            padding: 2px 0px 0px 5px;
            margin: 0px 40px 10px 20px;
            font-size: 12px;
        }
    </style>
</head>
<body>
<!--=======弹框开始=========-->
<!--遮罩层-->
<div class="zhy_zhezhao_pop"></div>
<!--遮罩层 end-->
<div class="zhy_popa gobackpop ">
    <p class="color66">温馨提示</p>
    <div class="pop_content">
        <p>是否保存当前页面已填写的内容？</p>
        <div class="operate_btn pull-right">
            <button type="button" class="btn cancle_yes" onclick="roleSubmit(this)">保存</button>
            <button type="button" class="btn cancle_btn" onclick="hideEdit('listRoles.action')">不保存</button>
        </div>
    </div>
</div>
<!--=======弹框结束=========-->
<div class="main_content create_site">
    <div class="col-xs-12 col-sm-12 div_bordered">
        <div class="panel panel-default choose_block">
            <div class="panel-heading color333">添加角色
                <a class="btn btn_blueg2 pull-right" href="javascript:void(0);" onclick="deleteM(this,'gobackpop')"
                   style="margin-right: 10px;">&lt;返回列表</a>
            </div>
            <div class="divider"></div>
            <div class="site_block member_block">
                <div class="well_head">
                    <div class="line"></div>
                    <span class="pull-left">基础信息</span>
                </div>
                <div class="panel-body">
                    <form class="form-horizontal" role="form" id="roleForm">
                        <div class="form-group">
                            <label for="" class="col-sm-2  control-label">角色名称：</label>
                            <div class="col-sm-10 ml150">
                                <input type="text" class="form-control input150 validate[maxSize[10],required]"
                                       data-prompt-position="inline" id="roleName" name="roleName" value=""
                                       placeholder="角色名称"><em class="colorred">*</em>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="" class="col-sm-2  control-label">英文名称：</label>
                            <div class="col-sm-10 ml150">
                                <input type="text" class="form-control input150 validate[maxSize[10],required]"
                                       name="optionId" data-prompt-position="inline" value="" placeholder="英文名称"/><em
                                    class="colorred">*</em>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="lastname" class="col-sm-2 control-label">角色描述：</label>
                            <div class="col-sm-10 ml150">
                                <textarea rows="4" class="form-control input300 validate[maxSize[200]]"
                                          data-prompt-position="inline"></textarea>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="well_head">
                    <div class="line"></div>
                    <span class="pull-left">权限分配</span>
                </div>
                <div class="panel-body  other">
                    <form class="form-horizontal zhanghao_js_add" id="moduleOpts" role="form">
                        <c:forEach items="${superviseList}" var="supervise">
                            <div class="form-group">
                                <label for="" class="control-label">
                                    <label class="label_check ">
                                        <input type="checkbox" onclick="setupAll(this)"/>${supervise.superviseTitle}：
                                    </label>
                                </label>
                                <div id="${supervise.superviseId}Div" class="switch_box" style="display: block;">
                                    <c:forEach items="${jurisdictionList}" var="jurisdiction">
                                        <c:if test="${fn:contains(supervise.superviseId,jurisdiction.superviseId)}">
                                            <div  class="switch_box pl20" >
                                                <label class="checkbox-inline">
                                                    <label class="label_check">
                                                        <input type="checkbox"
                                                               value="${jurisdiction.action}"
                                                               onclick="setupSingel(this)"/>${jurisdiction.actionTitle}
                                                    </label>

                                                </label>
                                            </div>
                                        </c:if>
                                    </c:forEach>
                                </div>
                            </div>
                        </c:forEach>
                        <div class="form-group add_block mb0">
                            <div class="divider"></div>
                            <div class=" col-sm-12 text-center">
                                <button type="button" class="btn btn_green" onclick="roleSubmit(this)">保存</button>
                                <button type="button" class="btn btn_whites"
                                        onclick="javascript:window.history.go(-1);">取消
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript" src="../../js/jquery-1.8.3.min.js"></script>
<!-- 省市区联动 e -->
<script type="text/javascript" src="../../js/common.js"></script>
<!--表单验证 js-->
<script type="text/javascript" src="../../js/jquery.validationEngine-zh_CN.js"></script>
<script type="text/javascript" src="../../js/jquery.validationEngine.js"></script>
<!-- json 工具 -->
<script type="text/javascript" src="../../js/json2.js"></script>
<script>
    /* function showHide(obj){
        if($(obj).attr("checked")){
            $(obj).parents(".form-group").find(":checkbox").each(function(){
                $(this).attr("checked","checked");
            });
        }else{
            $(obj).parents(".form-group").find(":checkbox").each(function(){
                $(this).removeAttr("checked");
            });
        }
        setupLabel();
    } */
    $(".form-horizontal").validationEngine({promptPosition: "bottomLeft:-10,0", scroll: false});

    function setupAll(obj) {
        if ($(obj).attr("checked")) {
            $(obj).parents(".control-label").next(".switch_box").find(":checkbox").each(function () {
                if (!$(this).prop("disabled")) {
                    $(this).attr("checked", "checked");
                }

            });
        } else {
            $(obj).parents(".control-label").next(".switch_box").find(":checkbox").each(function () {
                if (!$(this).prop("disabled")) {
                    $(this).removeAttr("checked");
                }

            });
        }
        setLevelOne()
        setupLabel();
    }

    function setupPartAll(obj) {
        if ($(obj).attr("checked")) {
            $(obj).parents(".checkbox-inline").next(".switch_box").find(":checkbox").each(function () {

                if (!$(this).prop("disabled")) {
                    $(this).attr("checked", "checked");
                }

            });
        } else {
            $(obj).parents(".checkbox-inline").next(".switch_box").find(":checkbox").each(function () {
                if (!$(this).prop("disabled")) {
                    $(this).removeAttr("checked");
                }

            });
        }
        setLevelOne()
        setupLabel();
    }

    function setupSingel(obj) {
        if ($(obj).parents(".pl20").length != 0) {
            var len_all = $(obj).parents(".pl20").find(":checkbox").length;//二级
            var len_checked = $(obj).parents(".pl20").find(":checkbox:checked").length;
            if (len_checked != 0) {
                $(obj).parents(".pl20").prev().find(":checkbox").attr("checked", "checked");
            } else {
                $(obj).parents(".pl20").prev().find(":checkbox").removeAttr("checked");
            }
        }
        setLevelOne()
        setupLabel();
    }

    function setLevelOne() {
        $(".zhanghao_js_add .form-group").each(function () {
            var len = $(this).children(".switch_box").find(":checkbox").length;
            var len_checked = $(this).children(".switch_box").find(":checkbox:checked").length;
            if (len_checked == len) {
                $(this).children("label").find(":checkbox").attr("checked", "checked");
            } else {
                $(this).children("label").find(":checkbox").removeAttr("checked");
            }
        });

        setupLabel();
    }

    setLevelOne();//默认设置
    $(function () {

        var chosenStr = '${chosenOpts}';
        //如果设置了角色的权限，设置选中状态
        if (chosenStr != '') {
            var _choseOpts = JSON.parse(chosenStr);
            $.each(_choseOpts, function (i, optVal) {
                var tag = $("input[value='" + optVal.moduleCode + ":" + optVal.optionCode + "']");
                if (tag) {
                    tag.prop("checked", true);
                    if (optVal.appRoleId > 0) {
                        tag.prop("disabled", true);
                    }
                }
            });
            //设置父级选中
            $("#moduleOpts").find(".form-group").each(function (i, _div) {
                var optsize = $(_div).find("div .checkbox-inline").length;
                var checkedNum = $(_div).find("input:checked").length;
                if (optsize == checkedNum) {
                    $(_div).find(".control-label .label_check input").prop("checked", true);
                }

            });

        }
        setupLabel();
    });


    //显示弹框
    function deleteM(obj, className, id) {
        $(".zhy_zhezhao_pop").show();
        var top = parent.document.body.scrollTop || parent.document.documentElement.scrollTop;
        var this_h = $("." + className).height();
        var win_h = ($('html', parent.document).height() - this_h - 100) / 2
        $(".zhy_zhezhao_pop").height($("html").height());
        top = top + win_h;
        $("." + className).css("top", top).show().siblings(".zhy_popa").hide();
    }

    // 隐藏修改弹框
    function hideEdit(url) {
        $(".zhy_zhezhao_pop").hide();
        $(".zhy_popa").hide();
        if (url) {
            window.location.href = url;
        }
    }

    //编辑角色
    function roleSubmit(_thisBtn) {
        hideEdit();
        if (!$("#roleForm").validationEngine("validate")) return;
        $(_thisBtn).attr('disabled', "true");
        //角色模块操作权限
        var actions = [];
        var flag = 0;
        //遍历所有的module
        $("#moduleOpts .form-group").each(function (i, modul) {
            //var moduleCode = $(modul).children("input[type='hidden']").val();
            //遍历module下选中的 操作选项
            $(modul).find(".switch_box input:checked").each(function (m, inpt) {
                flag = 1;
                if (!$(inpt).prop("disabled")) {
                    var _opCode = $(inpt).val();

                    actions.push(_opCode);
                }

            });
        });

        var optionId = $("input[name='optionId']").val();
        if (flag == 0) {
            zhyAlert("请给角色分配模块权限！");
            $(_thisBtn).removeAttr("disabled"); //移除disabled属性
            return;
        }
        $.ajax({
            type: "POST",
            url: "${basePath}/pages/role/editRole",
            dataType: "json",
            data: {
                "optionId": optionId == null ? 0 : optionId,
                "roleName": $("input[name='roleName']").val(),
                "roleDesc": $("textarea").val(),
                "actions": JSON.stringify(actions)
            },
            success: function (data) {
                //{"data":false,"resultCode":"SUCC"}
                if ($.trim(data)=="true") {
                        //跳转列表页
                        window.location.href = "${basePath}/pages/role/listRoles";
                    } else {
                        zhyAlert("角色名称不可与已有角色名称重复！");
                        $(_thisBtn).removeAttr("disabled"); //移除disabled属性
                    }
            }
        });
    }
</script>